package com.fdb.efp.console.api.rest;

import com.fdb.basic.framework.core.constant.DataRuleType;
import com.fdb.basic.framework.core.constant.PubConstant;
import com.fdb.basic.framework.core.constant.ResponseData;
import com.fdb.basic.framework.core.util.StringUtil;
import com.fdb.basic.framework.web.controller.AbstractController;
import com.fdb.efp.console.service.facade.SDutyService;
import com.fdb.efp.console.service.facade.SOrgService;
import com.fdb.efp.console.service.vo.SDutyVO;
import com.fdb.efp.console.service.vo.SOrgVO;
import com.fdb.efp.console.service.facade.SRoleDataRuleService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api")
public class SDutyController extends AbstractController  {
	
	@Autowired
	@Qualifier("sDutyService")
	private SDutyService sDutyService;

	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;

	@Autowired
	@Qualifier("sOrgService")
	private SOrgService sOrgService;
	
	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/s/dutys", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SDutyVO>> querySDutyAll(@RequestBody SDutyVO vo) {		
		// 根据用户角色获取用户权限方案
		List<SDutyVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = sDutyService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = sDutyService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = sDutyService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = sDutyService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);
		
	}
	/**
	 * 查询岗位信息
	 * @param vo
	 * @return
	 */
	@RequestMapping(value="/s/all/dutys", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SDutyVO>> queryAllSDuty(@RequestBody SDutyVO vo) {
		setUserInfoToVO(vo);
		List<SDutyVO> outputVo = sDutyService.queryAllDuty(vo);
		return getResponseData(outputVo);
		
	}
	
	
	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/s/duty/{dutyCode}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<SDutyVO> queryByPk(@PathVariable("dutyCode") String dutyCode) {		
		SDutyVO inVo = new SDutyVO();
		inVo.setDutyCode(dutyCode);
		SDutyVO outputVo = sDutyService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键删除信息
	 * @param sDuty
	 * @return
	 */
	@RequestMapping(value="/s/duty", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody SDutyVO inSDutyVo) {		
		int outputVo = sDutyService.deleteByPk(inSDutyVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 * @param inSDutyVo
	 * @return
	 */
	@RequestMapping(value="/s/duty", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody SDutyVO inSDutyVo) {
		setUserInfoToVO(inSDutyVo);
		/**
		 * 【2019D0519】【(问题编号)】 判断岗位是否重复
		 * 【解决方案】 判断岗位名称（精确匹配）是否在新增的适用机构，或其上下级机构存在。若存在，则为重复
		 */
		//获取适用机构关联的上下级机构信息
		List<SOrgVO> sOrgVOList = sOrgService.queryAllByOrgCode(inSDutyVo.getOrgCode());
		if (Objects.nonNull(sOrgVOList)) {
			List<String> locationList = sOrgVOList.stream()
					.map(SOrgVO::getLocation)
					.collect(Collectors.toList());
			//转为字符串并去重
			String orgLocation = this.getOrgLocations(locationList);
			if (StringUtil.isStrNotEmpty(orgLocation)) {
				inSDutyVo.setOrgsLocation(orgLocation);
				List<SDutyVO> sDutyVOList = sDutyService.queryDutyByOrgs(inSDutyVo);
				if (Objects.nonNull(sDutyVOList) && sDutyVOList.size() > 0) {
					//根据岗位编号排除自身
					int num = sDutyVOList.stream()
							.filter(sDutyVO -> {
								return !StringUtils.equals(inSDutyVo.getDutyCode(), sDutyVO.getDutyCode());
							})
							.collect(Collectors.toList())
							.size();
					if (num > 0)
						return getResponseData(-11);
				}
			}
		}
		int outputVo = sDutyService.updateByPk(inSDutyVo);
		return getResponseData(outputVo);
	}
	
	/**
	 * 新增数据
	 * @param inSDutyVo
	 * @return
	 */
	@RequestMapping(value="/s/duty", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertSDuty(@RequestBody SDutyVO inSDutyVo) {
		setUserInfoToVO(inSDutyVo);
		/**
		 * 【2019D0519】【(问题编号)】 判断岗位是否重复
		 * 【解决方案】 判断岗位名称（精确匹配）是否在新增的适用机构，或其上下级机构存在。若存在，则为重复
		 */
		//获取适用机构关联的上下级机构信息
		List<SOrgVO> sOrgVOList = sOrgService.queryAllByOrgCode(inSDutyVo.getOrgCode());
		if(Objects.nonNull(sOrgVOList)){
			List<String> locationList = sOrgVOList.stream()
					.map(SOrgVO::getLocation)
					.collect(Collectors.toList());
			//转为字符串并去重
			String orgLocation = this.getOrgLocations(locationList);
			if(StringUtil.isStrNotEmpty(orgLocation)){
				inSDutyVo.setOrgsLocation(orgLocation);
				List<SDutyVO> sDutyVOList = sDutyService.queryDutyByOrgs(inSDutyVo);
				if(Objects.nonNull(sDutyVOList) && sDutyVOList.size() > 0){
					return getResponseData(-11);
				}
			}
		}
		int outputVo = sDutyService.insertSDuty(inSDutyVo);
		return getResponseData(outputVo);
	}

	/**
	 * 查询当前登录法人机构下的所有岗位
	 * @param inSDutyVo
	 * @return
	 */
	@RequestMapping(value="/s/dutys/legal", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SDutyVO>> loginLegalSDutys(@RequestBody SDutyVO inSDutyVo) {
		setUserInfoToVO(inSDutyVo);
		List<SDutyVO> outputVo = sDutyService.queryLoginLegalSDutys(inSDutyVo);
		return getResponseData(outputVo);
	}
	
	@RequestMapping(value="/s/dutys/legal/status",method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SDutyVO>> queryDutyByLcAndStByPage(@RequestBody SDutyVO inSDutyVo){
		setUserInfoToVO(inSDutyVo);
		List<SDutyVO> outputVo = sDutyService.queryDutyByLcAndStByPage(inSDutyVo);
		return getResponseData(outputVo);
	}

	/**
	 * 角色下用户列表数据查询
	 * 
	 * @return
	 */
	@RequestMapping(value = "/s/dutys/user", method = RequestMethod.POST)
	public @ResponseBody ResponseData<List<SDutyVO>> queryDutyUserAll(@RequestBody SDutyVO inSDutyVo) {
		setUserInfoToVO(inSDutyVo);
		List<SDutyVO> outputVo = sDutyService.queryDutyUserAll(inSDutyVo);
		return getResponseData(outputVo);
	}

	@RequestMapping(value="/s/dutys/condition", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SDutyVO>> querySDutyCondition(@RequestBody SDutyVO vo) {
		setUserInfoToVO(vo);
		List<SDutyVO> outputVo = sDutyService.queryDutyAll(vo);
		return getResponseData(outputVo);
	}

	private String getOrgLocations(List<String> locations){
		List<String> result = new ArrayList<>();
		if(Objects.isNull(locations) || locations.isEmpty()){
			return null;
		}
		for(String location : locations){
			if(StringUtil.isStrNotEmpty(location)){
				if(location.contains(",")){
					String[] splits = location.split(",");
					for(int i=0; i<splits.length; i++){
						result.add(splits[i]);
					}
				}else {
					result.add(location);
				}

			}
		}
		if(result.size() > 0)
		    return result.stream().distinct().collect(Collectors.joining(","));
		else
			return null;
	}

}
